<?php
session_start();
include_once 'logging.inc.php';
if( !is_logged() ) {
  echo '{"result":false,"info":"You are not logged in"}';
  exit;
}
; // empty line seems to produce space...
// connect to database
include_once 'db_connection_info.inc.php';
$connexion = mysql_connect($host, $user, $password);
if (! $connexion) {
  echo '{"result":false,"info":"Unable to connect to the mysql server"}';
  exit;
} else {
  $ok = mysql_select_db($db, $connexion);
  if (! $ok) {
    echo '{"result":false,"info":"Unable to connect to the database"}';
    exit;
  }
}
; // empty line seems to produce space...
// execute the request
//
// FIXME: this is totally unsafe, it is just temporary code for starting
//        developing the rest.
$request=$_POST['request'];
$resultat =  mysql_query($request,  $connexion);
if ($resultat) {
  //  mysql_num_rows($resultat);
  $res = '{"result":true, "data":[';
  $first = true;
  while ($row = mysql_fetch_row($resultat)) {
    if(!$first) { $res=$res.','; }
    $first=false;
    $res = $res.json_encode($row);
  }
  $res = $res."]}";
  echo $res;
} else {
    echo json_encode( array (
      'result'=>false,'info'=>'Error in the request "'.$request.'"\n'
      ."MySql message: ". mysql_errno($connexion) .' - '.mysql_error($connexion)
    ) );
}

?>
